//
//  RichScanViewController.m
//  RENTVISITOR
//
//  Created by wonder on 2017/9/29.
//  Copyright © 2017年 UNN. All rights reserved.
//

#import "RichScanViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "RentVieitor.h"

@interface RichScanViewController ()<AVCaptureMetadataOutputObjectsDelegate>

@property (strong,nonatomic)AVCaptureDevice * device;
@property (strong,nonatomic)AVCaptureDeviceInput * input;
@property (strong,nonatomic)AVCaptureMetadataOutput * output;
@property (strong,nonatomic)AVCaptureSession * session;
@property (strong,nonatomic)AVCaptureVideoPreviewLayer * preview;

@end

@implementation RichScanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWidth, (KHeight-128-220)/2)];
    view.backgroundColor = [UIColor blackColor];
    view.alpha = 0.5;
    [self.view addSubview:view];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, (KHeight-128-220)/2+220, KWidth, (KHeight-128-220)/2+128)];
    view2.backgroundColor = [UIColor blackColor];
    view2.alpha = 0.5;
    [self.view addSubview:view2];
    
    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(0,(KHeight-128-220)/2 , (KWidth-220)/2, 220)];
    view3.backgroundColor = [UIColor blackColor];
    view3.alpha = 0.5;
    [self.view addSubview:view3];
    
    
    UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(KWidth-(KWidth-220)/2, (KHeight-128-220)/2, (KWidth-220)/2, 220)];
    view4.backgroundColor = [UIColor blackColor];
    view4.alpha = 0.5;
    [self.view addSubview:view4];

    
    [self leftView];
    [self creatView];
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,220,220)];
    CGPoint center = CGPointMake(self.view.center.x, self.view.center.y-64);
    imgView.center = center;
    imgView.image = [UIImage imageNamed:@"矩形-3（合并）"];
    [self.view addSubview:imgView];
    
     [_session startRunning];
    
}

- (void)returnImgBlock:(SelectnumberBlock)block {
    
    self.selecNumBlock = block;
}


- (void)leftView {
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWidth, 64)];
    view.backgroundColor = BackgroundColor;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 20, KWidth-200, 44)];
    label.text = @"二维码扫扫描";
    label.font = [UIFont systemFontOfSize:17];
    label.textColor = [Utils colorConvertFromString:@"#333333"];
    label.textAlignment = NSTextAlignmentCenter;
    [view addSubview:label];
    // 自定义导航栏左侧按钮
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(15, 20+12, 100,30);
    [leftBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 10, 90)];
    [leftBtn setImage:[UIImage imageNamed:@"箭头左"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:leftBtn];
    [self.view addSubview:view];
}

- (void)creatView {
    
    
    
    // Device
    _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    // Input
    _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device  error:nil];
    
    // Output
    _output = [[AVCaptureMetadataOutput alloc]init];
    [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    
    // Session
    _session = [[AVCaptureSession alloc]init];
    [_session setSessionPreset:AVCaptureSessionPresetHigh];
    
    if ([_session canAddInput:self.input])
    {
        [_session addInput:self.input];
    }
    
    if ([_session canAddOutput:self.output])
    {
        [_session addOutput:self.output];
    }
    
    _output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];
    
    _preview =[AVCaptureVideoPreviewLayer layerWithSession:_session];
    _preview.videoGravity =AVLayerVideoGravityResizeAspectFill;
    _preview.frame =self.view.layer.bounds;
    [self.view.layer insertSublayer:_preview atIndex:0];
    
    [_output setRectOfInterest:CGRectMake((124)/KHeight,((KWidth-220)/2)/KWidth,220/KHeight,220/KWidth)];
    

}

- (void)backAction {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)saoAction {
    
    
}


- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    NSString *stringValue;
    if ([metadataObjects count] >0){
        //停止扫描
        [_session stopRunning];
        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
        stringValue = metadataObject.stringValue;
        if (stringValue.length == 38) {
            
            NSString *str = [stringValue substringToIndex:31];
            NSString *number = [stringValue substringFromIndex:32];
            if ( [str isEqualToString:@"http://www.zuke.com/register?m="]) {
                
                if (self.selecNumBlock != nil) {
                    self.selecNumBlock(number);
                    [self dismissViewControllerAnimated:YES completion:nil];
                }
            }
        }
        

        
    }
}
@end
